package com.ruoyi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.cyb.botiancybcredit.pojo.CreditEntity;
import com.ruoyi.cyb.botiancybcredit.pojo.ValuationEntity;
import com.ruoyi.cyb.botiancyblogin.Result;
import com.ruoyi.cyb.botiancyblogin.ResultObject;
import com.ruoyi.cyb.botiancyblogin.pojo.UserEntity;
import com.ruoyi.mapper.CreditMapper;
import com.ruoyi.mapper.UserMapper;
import com.ruoyi.mapper.VluationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 26352
 * @Date 2023/3/23 15:3812
 * @Description TODO
 * @Version 1.0
 */
@Service
public class CreditService {

    /**
     * 操作用户信用分数
     */
    @Resource
    CreditMapper creditMapper;

    /**
     * 操作用户资产
     */
    @Resource
    VluationMapper vluationMapper;

    /**
     * 操作用户资产
     */
    @Resource
    UserMapper userMapper;




    /**
     * 查询用户资产总额 根据用户id
     * @return
     */
    public ResultObject selectByUid(Integer uid) {
        try {
            /**
             *         根据用户id查询 用户资产总金额
             */
            Double amount = vluationMapper.selectByUid(uid);

            /**
             * 判断资产额度给授信
             */
            if (amount<=0 || amount<30000 ){

                return new ResultObject<>(true,"0","此用户暂不符合授信要求",null);

            }

            /**
             *资产在大于30000 小于100000 信用分定为80分
             */
            if (amount>=30000 && amount < 100000){
                /**
                 * 信用分 80分
                 */
                QueryWrapper<CreditEntity> creditEntityQueryWrapper = new QueryWrapper<>();
                creditEntityQueryWrapper.lambda().eq(CreditEntity::getUid,uid);
                CreditEntity creditEntity = new CreditEntity();
                creditEntity.setCredit(80);
                creditMapper.update(creditEntity,creditEntityQueryWrapper);
                return  new ResultObject<>(true,"0","用户授信成功 请回个人中心查看",null);
            }

            /**
             *资产在大于100000 小于 150000 信用分定为90分
             */
            if (amount>=100000 && amount <150000){
                /**
                 * 信用分 90分
                 */
                QueryWrapper<CreditEntity> creditEntityQueryWrapper = new QueryWrapper<>();
                creditEntityQueryWrapper.lambda().eq(CreditEntity::getUid,uid);
                CreditEntity creditEntity = new CreditEntity();
                creditEntity.setCredit(90);
                creditMapper.update(creditEntity,creditEntityQueryWrapper);
                return  new ResultObject<>(true,"0","用户授信成功 请回个人中心查看",null);
            }

            /**
             *资产在大于150000 小于 200000 信用分定为95分
             */
            if (amount>=150000 && amount <200000){
                /**
                 * 信用分 95分
                 */
                QueryWrapper<CreditEntity> creditEntityQueryWrapper = new QueryWrapper<>();
                creditEntityQueryWrapper.lambda().eq(CreditEntity::getUid,uid);
                CreditEntity creditEntity = new CreditEntity();
                creditEntity.setCredit(95);
                creditMapper.update(creditEntity,creditEntityQueryWrapper);
                return  new ResultObject<>(true,"0","用户授信成功 请回个人中心查看",null);
            }

            /**
             *资产在大于300000 信用分定为96分
             */
            if (amount>=300000){
                /**
                 * 信用分 96分
                 */
                QueryWrapper<CreditEntity> creditEntityQueryWrapper = new QueryWrapper<>();
                creditEntityQueryWrapper.lambda().eq(CreditEntity::getUid,uid);
                CreditEntity creditEntity = new CreditEntity();
                creditEntity.setCredit(96);
                creditMapper.update(creditEntity,creditEntityQueryWrapper);
                return  new ResultObject<>(true,"0","用户授信成功 请回个人中心查看",null);
            }

        } catch (Exception e) {
            new ResultObject<>(false,"0","用户授信异常 请联系管理员处理",null);

        }

        return  new ResultObject<>(true,"0","用户授信成功 请回个人中心查看",null);

    }

    public ResultObject createUpper(Integer uid) {
        /**
         * 根据前台传来的用户id去查看用户是否存在
         */
        try {
            QueryWrapper<UserEntity> userEntityQueryWrapper = new QueryWrapper<>();
            userEntityQueryWrapper.lambda().eq(UserEntity::getId,uid);
            List<UserEntity> userEntities = userMapper.selectList(userEntityQueryWrapper);

            /**
             * 用户不存在返回提示
             */
            if (userEntities.size()<0){
                return  new ResultObject<>(false,"0","用户不存在请重新选择",null);
            }

            /**
             * 获取到用户信息
             */
            UserEntity userEntity = userEntities.get(0);

            /**
             * 查看用户信用分是否存在 不存在请返回
             */
            QueryWrapper<CreditEntity> creditEntityQueryWrapper = new QueryWrapper<>();
            creditEntityQueryWrapper.lambda().eq(CreditEntity::getUid,userEntity.getId());
            CreditEntity creditEntity = creditMapper.selectOne(creditEntityQueryWrapper);

            /**
             * 还没有授信
             */
            if (creditEntity.getCredit().intValue()==0){

                return  new ResultObject<>(false,"0","请先完成授信",null);

            }

            /**
             * 说明用户已有初始额度 不需要在生成初始额度
             */
            if (userEntity.getQuota()>1.0){
                return  new ResultObject<>(false,"0","以有初始额度 请回个人中心查看",null);
            }

            /**
             * 完成给用户的初始额度
             */
            if (creditEntity.getCredit().intValue()>=80 && creditEntity.getCredit().intValue()<90){
                UserEntity userEntity1 = new UserEntity();
                userEntity1.setQuota(10000.00);
                userMapper.update(userEntity1,userEntityQueryWrapper);

            }
            if (creditEntity.getCredit().intValue()>=90 && creditEntity.getCredit().intValue()<95){
                UserEntity userEntity1 = new UserEntity();
                userEntity1.setQuota(20000.00);
                userMapper.update(userEntity1,userEntityQueryWrapper);
                return  new ResultObject<>(true,"1","恭喜您获得初始额度 请回个人中心查看",null);

            }

            if (creditEntity.getCredit().intValue()>=95 && creditEntity.getCredit().intValue()<96){
                UserEntity userEntity1 = new UserEntity();
                userEntity1.setQuota(30000.00);
                userMapper.update(userEntity1,userEntityQueryWrapper);
                return  new ResultObject<>(true,"1","恭喜您获得初始额度 请回个人中心查看",null);

            }

            if (creditEntity.getCredit().intValue()>=96){
                UserEntity userEntity1 = new UserEntity();
                userEntity1.setQuota(50000.00);
                userMapper.update(userEntity1,userEntityQueryWrapper);
                return  new ResultObject<>(true,"1","恭喜您获得初始额度 请回个人中心查看",null);

            }
        } catch (Exception e) {
            new ResultObject<>(false,"0","获取初始额度异常 请联系管理员处理",null);
        }
        return  new ResultObject<>(true,"1","恭喜您获得初始额度 请回个人中心查看",null);
    }
}
